import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import cache from '@/utils/localStore'
// import { loginApi } from '@/service/userApi'
// export const fetchLoginAction = createAsyncThunk('user/login', async data => {
//   let res = await loginApi(data)
//   return res.data
// })

const userSlice = createSlice({
  name: 'uesr',
  initialState: {
    userinfo: {
      id: 0,
      name: '',
      token: ''
    }
  },
  reducers: {
    setUserinfo(state, { payload }) {
      state.userinfo = payload
      if (!cache().has('user')) {
        // 持久化
        cache().set('user', payload)
      }
    }
  }
  // extraReducers: builder => {
  //   builder.addCase(fetchLoginAction.fulfilled, (state, { payload }) => {
  //     state.userinfo = payload
  //   })
  // }
})

export const { setUserinfo } = userSlice.actions
export default userSlice.reducer
